package com.frejoys.store.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.PasswordChangeDto;
import com.frejoys.common.dto.app.CashPasswordDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.EncryptUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.CashPasswordStatusVo;
import com.frejoys.service.UserCashPasswordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "提现密码管理", description = "提现密码管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallUserCashPasswordController {

    private final UserCashPasswordService userCashPasswordService;

    private final RedissonClient redissonClient;

    @PostMapping("/userCashPassword/cashPassword")
    @Operation(summary = "查询用户是否设有密码")
    public Result<CashPasswordStatusVo> cashPassword(){
        return Result.success(userCashPasswordService.cashPassword(RequestUtil.userId()));
    }

    @PostMapping("/userCashPassword/addInfo")
    @Operation(summary = "添加提现信息密码")
    public Result addInfo(@RequestBody @Validated CashPasswordDto dto){

        RLock lock = redissonClient.getLock(CacheKeyUtil.addUserCashPasswordLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                userCashPasswordService.addInfo(RequestUtil.userId(), EncryptUtil.storeAes.decryptStr(dto.getPassword()));
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

    @PostMapping("/userCashPassword/upInfo")
    @Operation(summary = "修改提现信息密码")
    public Result upInfo(@RequestBody @Validated PasswordChangeDto dto){

        RLock lock = redissonClient.getLock(CacheKeyUtil.upUserCashPasswordLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                userCashPasswordService.upInfo(RequestUtil.userId(), EncryptUtil.storeAes.decryptStr(dto.getOldPassword()), EncryptUtil.appAes.decryptStr(dto.getNewPassword()));
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @PostMapping("/userCashPassword/verifyCashPassword")
    @Operation(summary = "校验提现账号信息密码")
    public Result verifyCashPassword(@RequestBody @Validated CashPasswordDto dto){
        return Result.success(userCashPasswordService.verifyCashPassword(RequestUtil.userId(), EncryptUtil.storeAes.decryptStr(dto.getPassword())));
    }
}
